package com.study.crud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.crud.model.user.User;
import com.study.crud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login/login";
    }

    @RequestMapping("/toRegister")
    public String toRegister(){
        return "login/register";
    }

    @RequestMapping("login")
    public String login(User user, Model model, RedirectAttributes redirectAttributes) {
        List<User> list = userService.list(new QueryWrapper<>());
        for (User u : list) {
            if (u.getUname().equals(user.getUname()) && u.getPwd().equals(user.getPwd())) {
                model.addAttribute("userId", u.getUid());
                //redirectAttributes.addAttribute("user",u);
                return "login/toIndex";
            }
        }
        return "error/error_login";
    }

    @RequestMapping("register")
    public String register(User user){
        return "error/error_register";//注册去error页面:去找公司
    }
}
